﻿using Management.Areas.Control.Controllers;
using Management.Areas.Control.Models;
using Management.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Management.Areas.Control
{
    public class InstallController : Controller
    {
        //
        // GET: /Control/Install/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index()
        {
            try
            {
                var roleService = new RoleManager();
                #region init permission
                CreatePermission(new AdminController());
                #endregion

                var allDefinedPermissions = roleService.GetDefinedPermissions();

                #region 管理员角色初始化
                var adminPermissions = new List<RolePermissionInfo>();
                foreach (var d in allDefinedPermissions)
                {
                    adminPermissions.Add(new RolePermissionInfo { Permission = d, });
                }
                int adminRoleId = roleService.AddRole(new Role
                {
                    Name = "管理员",
                    Description = "",
                    Permissions = adminPermissions,
                    AddDate = DateTime.Now,
                });
                #endregion
                return RedirectToAction("Admin", "Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View();
            }
        }
        private void CreatePermission(Controller customController)
        {
            var roleApi = new RoleManager();

            var controllerName = "";
            var controller = ""; var controllerNo = 0;
            var actionName = ""; var action = ""; var actionNo = 0;
            var controllerDesc = new KeyValuePair<string, int>();

            var controllerType = customController.GetType();

            //remove controller posfix
            controller = controllerType.Name.Replace("Controller", "");
            controllerDesc = Getdesc(controllerType);
            if (!string.IsNullOrEmpty(controllerDesc.Key))
            {
                controllerName = controllerDesc.Key;
                controllerNo = controllerDesc.Value;
                foreach (var m in controllerType.GetMethods())
                {
                    var mDesc = GetPropertyDesc(m);
                    if (string.IsNullOrEmpty(mDesc.Key)) continue;
                    action = m.Name;
                    actionName = mDesc.Key;
                    actionNo = mDesc.Value;
                    roleApi.CreatePermissions(actionNo, controllerNo, actionName, controllerName, controller, action);
                }
            }
        }
        private KeyValuePair<string, int> Getdesc(Type type)
        {
            var descriptionAttribute = (DescriptionAttribute)(type.GetCustomAttributes(false).FirstOrDefault(x => x is DescriptionAttribute));
            if (descriptionAttribute == null) return new KeyValuePair<string, int>();
            return new KeyValuePair<string, int>(descriptionAttribute.Name, descriptionAttribute.No);
        }
        private KeyValuePair<string, int> GetPropertyDesc(System.Reflection.MethodInfo type)
        {
            var descriptionAttribute = (DescriptionAttribute)(type.GetCustomAttributes(false).FirstOrDefault(x => x is DescriptionAttribute));
            if (descriptionAttribute == null) return new KeyValuePair<string, int>();
            return new KeyValuePair<string, int>(descriptionAttribute.Name, descriptionAttribute.No);
        }
	}
}